#include "stdafx.h"
#include "HMessageMaker.h"
#include "HActionDefines.h"


HMessageMaker::HMessageMaker()
{
}

HMessageMaker::~HMessageMaker()
{
}

QString HMessageMaker::MakeLoginMessage(QString const& userName)
{
    QJsonDocument doc;

    QJsonObject dataObject;
    dataObject.insert("name", userName);

    QJsonObject rootObject;
    rootObject.insert("action", (int)ActionType::kDataLogin);
    rootObject.insert("data", dataObject);

    doc.setObject(rootObject);
    return doc.toJson();
}

QString HMessageMaker::MakeQueryUserMessage(std::shared_ptr<LoginAction> pAction)
{
    QJsonDocument doc;

    QJsonObject rootObj = pAction->OutFieldAction();

    doc.setObject(rootObj);
    return doc.toJson();
}

QString HMessageMaker::MakeGetUserInfoListMessage(QString const& userName, QString const& token)
{
    QJsonDocument doc;

    std::shared_ptr<GetUserListAction> pAction = std::make_shared<GetUserListAction>();
    pAction->SetType(ActionType::kDataGetUserList);
    pAction->SetUserName(userName);
    pAction->SetToken(token);
    pAction->SetSuccess(false);
    QJsonObject rootObject = pAction->OutFieldAction();

    doc.setObject(rootObject);
    return doc.toJson();
}

QString HMessageMaker::MakeGetUserInfoListResultMessage(
    bool isOk,
    QString const& userName,
    QString const& token,
    QVector<UserInfo> const& userInfoList,
    QString const& errMsg)
{
    QJsonDocument doc;

    std::shared_ptr<GetUserListAction> pAction = std::make_shared<GetUserListAction>();
    pAction->SetType(ActionType::kDataGetUserListResult);
    pAction->SetUserName(userName);
    pAction->SetToken(token);
    pAction->SetUserInfoList(userInfoList);

    QJsonObject rootObject = pAction->OutFieldAction();

    doc.setObject(rootObject);
    return doc.toJson();
}

QString HMessageMaker::MakeQueryUserNameResultMessage(
    bool isOk,
    QString const& userName,
    QString const& errMsg)
{
    QJsonDocument doc;

    std::shared_ptr<LoginAction> pAction = std::make_shared<LoginAction>();
    pAction->SetSuccess(isOk);
    pAction->SetErrMsg(errMsg);
    pAction->SetType(ActionType::kDataQueryNameResult);
    pAction->SetUserName(userName);

    QJsonObject rootObject = pAction->OutFieldAction();

    doc.setObject(rootObject);
    return doc.toJson();
}

QString HMessageMaker::MakeLoginResultMessage(
    bool IsSuccess,
    QString const& userName,
    QString const& token,
    QString const& errMsg)
{
    QJsonDocument doc;

    std::shared_ptr<LoginAction> pAction = std::make_shared<LoginAction>();
    pAction->SetSuccess(IsSuccess);
    pAction->SetErrMsg(errMsg);
    pAction->SetType(ActionType::kDataLoginResult);
    pAction->SetUserName(userName);
    pAction->SetToken(token);

    QJsonObject rootObject = pAction->OutFieldAction();

    doc.setObject(rootObject);
    return doc.toJson();
}

QString HMessageMaker::MakeSendMessage(QString const& userName, QString const& token, QString const& msg)
{
    QJsonDocument doc;

    std::shared_ptr<UserMessageAction> pAction = std::make_shared<UserMessageAction>();

    pAction->SetType(ActionType::kDataMsg);
    pAction->SetUserName(userName);
    pAction->SetToken(token);
    pAction->SetPlanMessage(msg);

    QJsonObject rootObject = pAction->OutFieldAction();

    doc.setObject(rootObject);
    return doc.toJson();
}

QString HMessageMaker::MakeGetMessage(QString const& userName, QString const& msg)
{
    QJsonDocument doc;

    std::shared_ptr<UserMessageAction> pAction = std::make_shared<UserMessageAction>();

    pAction->SetType(ActionType::kGetDataMsg);
    pAction->SetUserName(userName);
    pAction->SetPlanMessage(msg);

    QJsonObject rootObject = pAction->OutFieldAction();

    doc.setObject(rootObject);
    return doc.toJson();
}
